import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';
import config from 'src/config';

@Injectable()
export class SocketJwtGuard implements CanActivate {
    // constructor(private authService: AuthService) {}

    async canActivate(context: ExecutionContext) {
		const data = context.switchToWs().getData();
		if(data.Authorization){
			const token = data.Authorization.split(' ')[1];
			if(token){
				const jwtPayload:any=jwt.verify(token, config.admin.secret);
				context.switchToWs().getData().userId = jwtPayload.id;
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}

    }
}
